package com.tongqu.handlers;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.tongqu.dispatch.DispatcherController;

import static io.netty.buffer.Unpooled.copiedBuffer;

@ChannelHandler.Sharable
@Component
public class WorkController extends SimpleChannelInboundHandler<FullHttpRequest> {


    Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private DispatcherController dispatcherController;

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, FullHttpRequest fullHttpRequest) throws Exception {

        if (fullHttpRequest != null) {
            dispatcherController.doDispath(channelHandlerContext, fullHttpRequest);
        }
    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        logger.error(cause.getMessage());
        cause.printStackTrace();
        ctx.close();
    }
}
